﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.Core.DBUtility;
using Microhard.Core;
using Microhard.Permission.Models;


namespace Microhard.Permission_MembershipImpl
{
    public class DictionaryImpl : Microhard.Permission.Interface.IDictionary
    {

        public Core.DBUtility.ExecutedResult Add(Permission.Models.DictionaryModel model)
        {
            ExecutedResult rtn = new ExecutedResult();
            MicrohardOUGDBDataContext db = new MicrohardOUGDBDataContext();
            MicroHard_Dictionary op = model.ToEntity();
            op.DataID = Guid.NewGuid();
            op.Value = Guid.NewGuid();
            op.CreateTime = DateTime.Now;
            try
            {
                db.MicroHard_Dictionaries.InsertOnSubmit(op);
                db.SubmitChanges();
                rtn.Scope.IsSysManage = true;
            }
            catch (System.Exception ex)
            {
                rtn.ErrorMsg.Add("DictionaryImpl.Add", "添加字典失败，请联系系统管理员");
                Log.Error("DictionaryImpl.Add", ex);
            }
            return rtn;
        }

        public Core.DBUtility.ExecutedResult Update(Permission.Models.DictionaryModel model)
        {
            ExecutedResult rtn = new ExecutedResult();
            MicrohardOUGDBDataContext db = new MicrohardOUGDBDataContext();
            var list = db.MicroHard_Dictionaries.SingleOrDefault(n => n.DataID == model.DataID);

            if (list != null)
            {
                list.Name = model.Name;
                list.Type = model.Type;
                list.Status = model.Status;
                try
                {
                    db.SubmitChanges();
                }
                catch (Exception ex)
                {
                    rtn.ErrorMsg.Add("DictionaryImpl.Update", "修改字典失败，请联系系统管理员");
                    Log.Error("DictionaryImpl.Update", ex);
                }

            }

            return rtn;
        }

        public Core.DBUtility.ExecutedResult Delete(List<Guid> ids)
        {
            ExecutedResult rtn = new ExecutedResult();
            MicrohardOUGDBDataContext db = new MicrohardOUGDBDataContext();
            var dics = from d in db.MicroHard_Dictionaries
                       where ids.Contains(d.DataID)
                       select d;
            foreach (var deldic in dics)
            {
                db.MicroHard_Dictionaries.DeleteOnSubmit(deldic);
            }
            try
            {
                db.SubmitChanges();
            }
            catch (Exception ex)
            {
                rtn.ErrorMsg.Add("DictionaryImpl.Delete", "删除字典失败，请联系系统管理员");
                Log.Error("DictionaryImpl.Delete", ex);
            }
            return rtn;
        }

        public Core.DBUtility.ExecutedResult Delete(Permission.Models.DictionaryModel model)
        {
            throw new NotImplementedException();
        }

        public Permission.Models.DictionaryModel GetModelByID(Guid id)
        {
            var db = new MicrohardOUGDBDataContext();
            var entity = db.MicroHard_Dictionaries.SingleOrDefault(d => d.DataID == id);
            if (entity != null)
                return entity.ToModel();
            return null;
        }

        public Permission.Models.DictionaryModel GetModelByModel(Permission.Models.DictionaryModel model)
        {
            throw new NotImplementedException();
        }

        public List<Permission.Models.DictionaryModel> Query(Permission.Models.DictionaryModel Model, int pageIndex, int pageSize, out int totalCount)
        {
            List<DictionaryModel> rtn = new List<DictionaryModel>();
            var db = new MicrohardOUGDBDataContext();
            IEnumerable<MicroHard_Dictionary> data = from _Data in db.MicroHard_Dictionaries
                                                     select _Data; ;
            if (Model != null && !string.IsNullOrEmpty(Model.Name))
            {
                data = data.Where(n => n.Name == Model.Name);
            }
            //数据总记录数  
            totalCount = data.Count();
            rtn = data.OrderBy(p => p.Type).Skip((pageIndex - 1) * pageSize).Take(pageSize).Select(p => p.ToModel()).ToList();
            return rtn;
        }

        public List<Permission.Models.DictionaryModel> Query(Permission.Models.DictionaryModel Model)
        {
            var db = new MicrohardOUGDBDataContext();
            List<Permission.Models.DictionaryModel> dics = null;
            if (Model == null)
            {
                dics = db.MicroHard_Dictionaries.Select(d => d.ToModel()).ToList();
            }
            else if (!string.IsNullOrEmpty(Model.Type))
            {
                dics = db.MicroHard_Dictionaries
                    .Where(d => d.Type == Model.Type && d.Status == true)
                    .ToList().Select(d => d.ToModel()).ToList();
            }
            else if (!string.IsNullOrEmpty(Model.Name))
            {
                dics = db.MicroHard_Dictionaries
                    .Where(d => d.Name == Model.Name && d.Status == true)
                    .ToList().Select(d => d.ToModel()).ToList();
            }
            return dics;
        }

        public Core.DBUtility.ScopeResult scope
        {
            set;
            get;
        }

    }
}
